<template>
  <div class="audioBook" v-title :data-title="$t('router.audioBookTitle')">
    <div class="headNav scrollbar">
      <el-menu
        :default-active="onRoutes"
        class="el-menu-demo"
        mode="horizontal"
        text-color="#000"
        active-text-color="#A1322C"
        @select="handleSelect"
        router
      >
        <el-menu-item index="all">
          <span>{{ $t("public.all") }}</span>
        </el-menu-item>
        <template v-for="item in navData">
          <el-menu-item :index="item.categoryId + ''" :key="item.categoryId">
            <span
              v-text="
                $i18n.locale === 'zh' ? item.categoryCname : item.categoryName
              "
            ></span>
          </el-menu-item>
        </template>
        <li class="el-icon-arrow" v-if="!isMobile">
          <i
            class="el-icon-arrow-down"
            @click="getCategoryList"
            v-if="isArrow"
          ></i>
          <i class="el-icon-arrow-up" @click="getCategoryPage" v-else></i>
        </li>
      </el-menu>
    </div>
    <ul
      class="list"
      v-if="list !== undefined && list.length > 0"
      v-infinite-scroll="load"
      infinite-scroll-disabled="isPage"
      infinite-scroll-delay="120"
      infinite-scroll-distance="10"
      infinite-scroll-immediate="false"
    >
      <li
        v-for="item of list"
        @click="toDetail(item)"
        :key="item.catalogueId"
        class="item"
      >
        <div class="cover">
          <div class="image">
            <el-image
              :key="item.catalogueId"
              :src="item.cover500 || item.cover200"
              fit="cover"
            >
              <div
                slot="placeholder"
                class="image-slot flex-center flex-column"
              >
                <i class="iconfont el-icon-loading"></i>
              </div>
              <div slot="error" class="image-slot flex-center">
                <img src="@/assets/images/defaultCover.png" />
              </div>
            </el-image>
          </div>
        </div>
        <!-- @click="toDetail(item.videoId ? item.videoId : item.catalogueId)" -->
        <div class="info">
          <h2
            class="title ellipsis-two"
            v-text="
              $i18n.locale === 'zh'
                ? item.catalogueCname || item.catalogueName
                : item.catalogueName
            "
          ></h2>
        </div>
      </li>
      <li class="item"></li>
      <li class="item"></li>
      <li class="item"></li>
    </ul>
    <nice-empty
      v-show="list.length == 0 && fullscreenLoading == false"
      :emptyText="$t('public.noData')"
    ></nice-empty>
    <div v-if="loading" class="load-bottom">
      <nice-loading />
    </div>
    <div class="page-wrap" v-show="isPage">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        background
        hide-on-single-page
        layout="total, prev, pager, next, jumper"
        :total="total"
        :small="isMobile"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
// import videoList from "@/components/common/videoList/Index";
export default {
  data() {
    return {
      isArrow: true,
      navData: [],
      list: [],
      // 是否显示加载动画
      loading: false,
      // 请求参数
      params: {
        // 返回数量
        pageSize: 30,
        // 当前页数
        pageNum: 1,
        categoryId: this.$route.params.id == "all" ? "" : this.$route.params.id,
      },
      //数据载入动画
      fullscreenLoading: true,
      //是否显示分页
      isPage: false,
      total: 0, //总条数
    };
  },
  components: {
    // videoList,
  },
  computed: {
    onRoutes() {
      return this.$route.params.id;
    },
    isMobile() {
      return this.$store.getters.isMobile;
    },
    isIE() {
      return this.$store.getters.isIE;
    },
  },
  watch: {},
  created() {},
  mounted() {
    if (this.isMobile) {
      this.getCategoryList();
    } else {
      if (this.isIE) {
        this.params.pageSize = 50;
      }
      this.getCategoryPage();
    }
  },
  methods: {
    handleSelect(val) {
      this.params.pageNum = 1;
      this.params.categoryId = val == "all" ? "" : val;
      this.isPage = false;
      this.list = [];
      this.getCategoryAlbum();
    },
    //分页 每页条数
    handleSizeChange(val) {
      this.params.pageSize = val;
      this.getCategoryAlbum();
    },
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.list = [];
      this.getCategoryAlbum();
    },
    // 获取有声读物分类 -分页
    async getCategoryPage() {
      this.isArrow = true;
      try {
        let params = {
          pageNum: 1,
          pageSize: 10,
        };
        let res = await this.$api.audioBooks.getCategoryPage(params);
        if (res.code === 1000000) {
          this.navData = res.source ? res.source : [];
        }
        this.getCategoryAlbum();
      } catch (e) {
        console.log(e);
      }
    },
    // 获取有声读物分类 All
    async getCategoryList() {
      this.isArrow = false;
      try {
        let res = await this.$api.audioBooks.getCategoryList({});
        if (res.code === 1000000) {
          this.navData = res.source ? res.source : [];
        }
        this.getCategoryAlbum();
      } catch (e) {
        console.log(e);
      }
    },
    // 获取专辑列表
    async getCategoryAlbum() {
      this.fullscreenLoading = true;
      try {
        let res = await this.$api.audioBooks.getCategoryAlbum(this.params);
        if (res.code === 1000000) {
          // this.list = this.list.concat(
          //   this._normalizeVideos(res.data)
          // );
          this.list = res.source
            ? this.utils.processingData(this.list, res.source)
            : [];
          this.total = res.total;
          this.fullscreenLoading = false;
          if (res.size < 48 && res.size < res.total) {
            this.loading = true;
            this.isPage = false;
          } else {
            this.loading = false;
            this.isPage = true;
          }
        }
      } catch (e) {
        console.log(e);
      }
    },
    toDetail(item) {
      let params = {
        sort: "audio-book",
        id: item.catalogueId,
      };
      this.$router.push({
        name: this.isMobile
          ? "mAudioBooksAlbumDetail"
          : "audioBooksAlbumDetail",
        params,
      });
    },
    // 加载更多
    load() {
      if (this.isMobile) {
        this.params.pageSize += 18;
      } else {
        this.params.pageSize += 20;
      }
      this.getCategoryAlbum();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-icon-arrow {
  float: right;
  margin-top: 15px;
  cursor: pointer;
  i {
    font-size: 26px;
    font-weight: bold;
  }
}
.headNav {
  margin-bottom: 20px;
}
.list {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 0 -10px;
  .item {
    flex: 0 0 20%;
    max-width: 20%;
    padding: 0 10px;
    margin-bottom: 30px;
    cursor: pointer;
    .cover {
      width: 100%;
      height: 224px;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      .el-image {
        width: 224px;
        height: 224px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .info {
      font-size: 20px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      line-height: 28px;
      margin-top: 10px;
    }
  }
}
</style>
